﻿using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System;

namespace SharePointUtilitiesForDevelopers.Mapper
{
    /// <summary>
    /// A mapper that maps the xml web service response to a <see cref="SharePointContentType"/>.
    /// </summary>
    public class SharePointContentTypeMapper : IMapper<XElement, SharePointContentType>
    {
        private readonly IMapper<XElement, SharePointField> _fieldMapper;

        /// <summary>
        /// Initializes a new instance of the <see cref="SharePointContentTypeMapper"/> class.
        /// </summary>
        /// <param name="fieldMapper">The field mapper.</param>
        public SharePointContentTypeMapper(IMapper<XElement, SharePointField> fieldMapper)
        {
            _fieldMapper = fieldMapper;
        }

        /// <summary>
        /// Maps the source type to the target type.
        /// </summary>
        /// <param name="from">The object to map from.</param>
        /// <returns>The mapped object.</returns>
        public SharePointContentType Map(XElement from)
        {
            return MapList(from).SingleOrDefault();
        }
        
        /// <summary>
        /// Maps the source type to a list of the target type.
        /// </summary>
        /// <param name="from">From.</param>
        /// <returns></returns>
        public IEnumerable<SharePointContentType> MapList(XElement from)
        {
            try
            {
                var contentTypes = SharePointMapper.MapResponseToList<SharePointContentType>(
                    MapInternal,
                    element => string.Compare(element.Name.LocalName, "ContentType", StringComparison.InvariantCultureIgnoreCase) == 0,
                    from).OrderBy(ct => ct.Name).ToList();

                return contentTypes;
            }
            catch (Exception ex)
            {
                throw new SharePointMapperException<XElement, IEnumerable<SharePointContentType>>(ex);
            }
        }

        /// <summary>
        /// Maps the element to a domain object.
        /// </summary>
        /// <param name="element">The element to map from.</param>
        /// <returns>The mapped object.</returns>
        private SharePointContentType MapInternal(XElement element)
        {
            return new SharePointContentType
                       {
                           Id = SharePointXml.GetAttributeAsString(element, "ID"),
                           Name = SharePointXml.GetAttributeAsString(element, "Name"),
                           Description = SharePointXml.GetAttributeAsString(element, "Description"),
                           Group = SharePointXml.GetAttributeAsString(element, "Group"),
                           Definition = element,
                           Sealed = SharePointXml.GetAttributeAsBoolean(element, "Sealed"),
                           Hidden = SharePointXml.GetAttributeAsBoolean(element, "Hidden"),
                           Fields = _fieldMapper.MapList(element).ToList().AsReadOnly()
                       };
        }
    }
}
